package Well_Game;

import java.util.Scanner;

class Well_Game {
    private  int[][] well;

    Well_Game(){
        well = new int[3][3];
        initialize();
    }

    private int getWell(int i, int j) {
        if(i > 2 || i < 0 || j > 2 || j < 0)
            return -1;
        return well[i][j];
    }

    private void setWell(int i, int j, int num) {
        well[i][j] = num;
    }

    private void initialize() {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                setWell(i,j,0);
    }
    private void showboard() {
        System.out.println("\n-------------");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++){
                System.out.print("|");
                switch (getWell(i,j)){
                    case 0 : System.out.print("   ");break;
                    case 1 : System.out.print(" X ");break;
                    case 2 : System.out.print(" O ");break;
                }
            }
            System.out.println("|");
            System.out.println("-------------");
        }
    }

    private boolean setchessman(int i, int j, int player){
        switch (getWell(i,j)){
            case 0 : { setWell(i, j, player);return false; }
            case -1: { System.out.println("Your input out of range"); return true;}
            default: { System.out.println("This cell is already occupied. Try a different cell");return true; }
        }
    }
    void game(){
        int step = 0;
        showboard();
        do {
            turn(step);
            step++;
        } while (!judge(step));
    }
    private void turn(int step){
        int i,j,player;
        if (step % 2 == 0)
            player = 1;
        else
            player = 2;
        Scanner input = new Scanner(System.in);
        do {
            System.out.print("Enter a row for player" + (player == 1? "X:" : "O:"));
            i = input.nextInt();
            System.out.print("Enter a column for player" + (player == 1? "X:" : "O:"));
            j = input.nextInt();
        }
        while (setchessman(i, j, player));
        showboard();
    }
    private boolean judge(int step){
        for (int i = 0; i < 3; i++) {
            if(getWell(i,0) == 1 && getWell(i,1) == 1 && getWell(i,2) == 1) {
                System.out.println("X player won");
                return true;
            }
            else if(getWell(i,0) == 2 && getWell(i,1) == 2 && getWell(i,2) == 2) {
                System.out.println("O player won");
                return true;
            }
        }
        for (int j = 0; j < 3; j++) {
            if(getWell(0,j) == 1 && getWell(1,j) == 1 && getWell(2,j) == 1) {
                System.out.println("X player won");
                return true;
            }
            else if(getWell(0,j) == 2 && getWell(1,j) == 2 && getWell(2,j) == 2) {
                System.out.println("O player won");
                return true;
            }
        }
        if(getWell(1,1) != 0){
            if(getWell(0,0) == 1 && getWell(1,1) == 1 && getWell(2,2) == 1 ||
                    getWell(0,2) == 1 && getWell(1,1) == 1 && getWell(2,0) == 1 ) {
                System.out.println("X player won");
                return true;
            }
            else if(getWell(0,0) == 2 && getWell(1,1) == 2 && getWell(2,2) == 2 ||
                    getWell(0,2) == 2 && getWell(1,1) == 2 && getWell(2,0) == 2 ) {
                System.out.println("O player won");
                return true;
            }
        }
        if(step == 9) {
            System.out.println("No winner");
            return true;
        }
        return false;
    }
}
